<%@ page import="org.gnomus.server.Context" %>
<%@ page import="com.google.appengine.api.users.UserService" %>
<%@ page import="com.google.appengine.api.users.UserServiceFactory" %>
<%
  Context ctx = Context.get();
  UserService us = UserServiceFactory.getUserService();
  Boolean logged_in = us.getCurrentUser() != null;
%>
<script>
  function validateSignIn() {
    var form = $("#sign_in_widget");
    var valid = validate(form);
    if (valid) {
      var password = form.find("input[name=password]")[0];
      var sha1pass = form.find("input[name=sha1pass]")[0];
      sha1pass.value = hex_sha1(password.value);
      password.value = "";
    }
    return valid;
  }
</script>
<form id="sign_in_widget" method="post" action="/user/sign_in" onsubmit="return validateSignIn();">
  <table>
    <tr>
      <td colspan="2">
        <label style="font-size: 18px;">
          Sign in with your e-mail
        </label>
      </td>
    </tr>
    <tr>
      <td class="a-right">
        <label for="email">
          Email:
        </label>
      </td>
      <td>
        <input type="text" name="email">
      </td>
    </tr>
    <tr>
      <td>
        <label for="password">
          Password:
        </label>
      </td>
      <td>
        <input type="password" name="password">
        <input type="hidden" name="sha1pass" value="">
      </td>
    </tr>
    <tr class="remember-row">
      <td class="a-right">
        <input type="checkbox" name="remember">
      </td>
      <td class="a-left">
        <label for="remember">
          Stay signed in?
        </label>
      </td>
    </tr>
    <tr>
      <td>
        <input type="hidden" name="b" value="<%= ctx.uri %>">
      </td>
      <td class="a-left">
        <input type="submit" value="Sign In">
      </td>
    </tr>
    <tr>
      <td colspan="2">
        <%
          if (logged_in) {
        %>
        <a class="small" href="<%= us.createLogoutURL(ctx.uri) %>">
          Logout
        </a>
        <%
          } else {
        %>
        <a class="small" href="<%= us.createLoginURL(ctx.uri) %>">
          Login using your google account
        </a>
        <%
          }
        %>
      </td>
    </tr>
  </table>
</form>